package gsp420.helloworld;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
// extends <class, ONLY one> implements <interface, as many as your like>
public class Gooey extends JPanel implements MouseListener, Runnable {
	// 				c++		java
	// char			1		1 or 2
	// byte					1
	// int			4*		4
	// long			4		8
	// long long	8
	// short		2		2
	// float 		4		4
	// double		8		8
	// bool			1
	// boolean				1
	
	Thread t;
	
	public Gooey(){
		addMouseListener(this);
		t = new Thread(this);
		t.start();
	}
	
	private static final long serialVersionUID = 1L;
	public static void main(String[] args){
		JFrame jf = new JFrame("Title");
		Gooey goo = new Gooey();
		jf.add(goo);
		jf.setSize(800, 600);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
//		g.setColor(Color.white);
//		g.fillRect(0, 0, getWidth(), getHeight());
//		g.setColor(Color.black);

		g.fillRect(100, 200, 300, 400);	// r, 100, 200, 300, 400
		g.drawString("Hello World!", 50, 200);
//		g.setColor(Color.red);
		g.setColor(new Color(255, 128, 0));	// orange
		g.drawLine(50, 200, 500, 10);
		int rad = 10;
		g.drawOval((int)mouseClick.x-rad, (int)mouseClick.y-rad, rad*2, rad*2);
		if(ball != null)
			ball.draw(g);
	}
	public void mouseClicked(MouseEvent arg0) {
		System.out.println("click");
	}
	public void mouseEntered(MouseEvent arg0) {
		System.out.println("enter");
	}
	public void mouseExited(MouseEvent arg0) {
		System.out.println("exit");
	}
	public void mousePressed(MouseEvent arg0) {
		System.out.println("press");
		mouseClick.set(arg0.getX(), arg0.getY());
		if(ball == null){
			ball = new Ball(10, mouseClick);
		}else{
			ball.pos.set(mouseClick);
			ball.v.set(0,0);
		}
		repaint();
	}
	public void mouseReleased(MouseEvent arg0) {
		System.out.println("release");
	}
	
	Vector2 mouseClick = new Vector2();
	/** java automatically clears memory if no initialization is set */
	Ball ball;
	boolean m_running;

	public void run() {
		long now, then = System.currentTimeMillis(), passed;
		m_running = true;
		while(m_running){
			// timer code
			now = System.currentTimeMillis();
			passed = now - then;
			then = now;
//			System.out.println(passed);
			// update code
			float seconds = passed/1000.0f;
			update(seconds);
			// draw code
			repaint();	// sometime in the future, please draw... it would be cool.
			try{Thread.sleep(10);}catch(Exception e){}
		}
	}
	
	private void update(float seconds){
		if(ball != null){
//			System.out.println(ball.pos.x+"  "+ball.pos.y);
			// move the ball
			ball.pos.x += ball.v.x*seconds;
			ball.pos.y += ball.v.y*seconds;
			// accelerate due to gravity
			ball.v.y += 100*seconds;
			// if the ball reaches the ground, move it resting on the ground, and kill velocity
			if(ball.pos.y+ball.radius >= getHeight()){
				ball.pos.y = getHeight()-ball.radius;
				if(ball.v.y > 2){
					ball.v.y *= -0.5;
				}else{
					ball.v.y = 0;
				}
			}
		}
	}
}
